package com.neulogistics.contrib.procuredcenter.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.procuredcenter.api.application.command.CreateVendorCommand;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.Vendor;
import com.neulogistics.contrib.procuredcenter.domain.repository.IVendorRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CreateVendorCommandHandler implements IRequestHandler<CreateVendorCommand, BaseResponse<String>> {
    @Resource
    IVendorRepository vendorRepository;

    @Override
    public BaseResponse<String> handle(CreateVendorCommand command) throws Exception {
        Vendor vendor = new Vendor();

        BeanUtils.copyProperties(command, vendor);

        vendorRepository.save(vendor);

        return ResultUtils.success(vendor.getVendorId().toString());
    }
}
